package com.example.springstudyvue.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springstudyvue.entity.Book;
import com.example.springstudyvue.mapper.BookMapper;
import com.example.springstudyvue.result.Result;
import com.example.springstudyvue.service.BookService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  书籍管理控制器
 * </p>
 *
 * @author linKe
 * @since 2021-11-26
 */
@RestController
@RequestMapping("/book")
public class BookController  {
    @Resource
    private BookService bookService;
    @Resource
    private BookMapper bookMapper;





    @ApiOperationSupport(author = "linKe 的新增书籍接口")
    @ApiOperation(value="根据书籍信息新增",notes="返回操作结果")
    @PostMapping()
    public Result<Boolean> addBook(@RequestBody Book book) {
        return Result.success(bookService.addBook(book));
    }

    @ApiOperation(value="根据书籍更新数据",notes="返回操作结果")
    @PutMapping()
    public Result<Boolean> updateBook(@RequestBody Book book) {
        return Result.success(bookService.updateBook(book));
    }

    @ApiOperation(value="根据书籍ID删除",notes="返回操作结果")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteBook(@PathVariable("id") Long bookId) {
        return Result.success(bookService.deleteBook(bookId));
    }






    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search",defaultValue = "") String search) {

        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery();
        Page<Book> bookPage = new Page<>(pageNum, pageSize);

        //huTool的工具包 判断search不是空的前提下
        if (StrUtil.isNotBlank(search)) {
            //Book::getNickName 调用book的nickName字段   Book ::getAccount 或者账号名
            wrapper.like(Book::getName, search);
        }
        Page<Book> bookPages = bookMapper.selectPage(bookPage, wrapper);

        return new Result<>("200","ok",bookPages);
    }










}